#!/bin/bash
if [ -z "${MountPoint}" ]; then
    source ./0_env.sh
fi

# compare arg1 and arg2
function compare_num()
{
    if (($# != 2)); then echo "Invalid args num"; return 1; fi
    if [ -z $1 ]; then echo "arg 1 is null"; return 1; fi
    if [ -z $2 ]; then echo "arg 2 is null"; return 1; fi

    #将变量中所有数字替换掉后判断变量是否为空，如果不为空则不为纯数字返回 error
    if [ -n "`echo $1 | sed 's/[0-9]//g'`" ]; then echo "Not number";return 1;fi
    if [ -n "`echo $2 | sed 's/[0-9]//g'`" ]; then echo "Not number";return 1;fi

    if (($1 != $2)); then echo "Error: not equal";return 1; else echo -e "\tOk"; fi
    return 0;
}

# arg1: dirpath 
# arg2: expect sys_du_size
# arg3: "--uid=x or --gid=x"
function test_du_size_dir()
{
    echo -e "\n\t测试目录大小准确性 orcafs-ctl --du --path=$1 --type=dir $3"
    if (($# < 2)); then echo "Invalid args num"; return 1; fi

    dirPath="$1"

    ctl_du_size=`orcafs-ctl --du --path="${dirPath}" --type=dir $3 | awk '{print $5}'`
    sys_du_size=$2
    echo -e "\t ctl_du_size=${ctl_du_size}, sys_du_size=${sys_du_size}"
    
    compare_num "${ctl_du_size}" "${sys_du_size}"
}

# arg1: dirpath 
# arg2: expect sys_du_size
# arg3: "--uid=x or --gid=x"
function test_du_size_file()
{
    echo -e "\n\t测试文件大小准确性 orcafs-ctl --du --path=$1 --type=file $3"
    if (($# < 2)); then echo "Invalid args num"; return 1; fi

    dirPath="$1"
    idStr="$3"

    ctl_du_size=`orcafs-ctl --du --path=${dirPath} --type=file ${idStr} | awk '{print $5}'`
    sys_du_size=$2
    echo -e "\t ctl_du_size=${ctl_du_size}, sys_du_size=${sys_du_size}"

    compare_num "${ctl_du_size}" "${sys_du_size}"
}

# arg1: dirpath 
# arg2: expect sys_du_size
# arg3: "--uid=x or --gid=x"
function test_du_size_all()
{
    echo -e -n "\n\t测试总容量准确性 orcafs-ctl --du --path=$1 $3"
    if (($# < 2)); then echo "Invalid args num"; return 1; fi

    dirPath="$1"

    ctl_du_size=`orcafs-ctl --du --path="${dirPath}" $3 | awk '{print $5}'`
    sys_du_size=$2
    echo -e "\tctl_du_size=${ctl_du_size}, sys_du_size=${sys_du_size}"
    
    compare_num "${ctl_du_size}" "${sys_du_size}"
}